package com.tianlu.store.controller;

import cn.hutool.core.util.StrUtil;
import com.tianlu.store.common.*;
import com.tianlu.store.entity.ProductInfo;
import com.tianlu.store.enums.ProductTypeEnum;
import com.tianlu.store.service.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author 梁东烂
 * @Desc 商品信息表(ProductInfo)表控制层
 * @Date 2023-04-03 16:50:55
 */
@Api(tags = "商品信息表")
@RestController
@RequestMapping("/api/productInfo")
@RequiredArgsConstructor
public class ProductInfoController {
	@Resource
    private ProductInfoService productInfoService;
 
	
	@ApiOperation("商品信息表列表(分页）接口")
    @PostMapping("/findPage")
    @NoCheckToken
    public ResponseResult<PageResult<ProductInfo>> findPage(@RequestBody ProductInfo productInfo) {
        return ResponseResult.success(productInfoService.findPage(productInfo));
    }
	
	@ApiOperation("商品信息表列表(不分页）接口")
    @PostMapping("/findList")
    @NoCheckToken
    public ResponseResult<List<ProductInfo>> findList(@RequestBody ProductInfo productInfo) {
        return ResponseResult.success(productInfoService.findList(productInfo));
    }
	
	@ApiOperation("查看商品信息表接口")
    @GetMapping(value = "/detail")
    @NoCheckToken
    public ResponseResult<ProductInfo> detail(@RequestParam Long id) {
        return ResponseResult.success(productInfoService.getById(id));
    }

	@ApiOperation("添加商品信息表接口")
    @PostMapping("/save")
    public ResponseResult<Boolean> save(@RequestBody ProductInfo productInfo) {
        productInfo.setId(IdGen.snowflakeId());
        if (ProductTypeEnum.SECKILL.value.equals(productInfo.getType())) {
            if (Objects.isNull(productInfo.getBeginTime()) || Objects.isNull(productInfo.getKillPrice())) {
                throw new ServiceException(ResultCode.CODE_400, "秒杀价和秒杀开始时间不能为空！");
            }
            productInfo.setAgentPrice(productInfo.getKillPrice());
            productInfo.setCompanyPrice(productInfo.getKillPrice());
        } else if (ProductTypeEnum.FREE.value.equals(productInfo.getType())) {
            productInfo.setAgentPrice(productInfo.getPrice());
            productInfo.setCompanyPrice(productInfo.getPrice());
        }
        return ResponseResult.success(productInfoService.save(productInfo));
    }
	
	@ApiOperation("修改商品信息表接口")
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody ProductInfo productInfo) {
        if (Objects.isNull(productInfo.getId())) {
            return ResponseResult.fail(ResultCode.CODE_400, "ID不能为空");
        }
        if (ProductTypeEnum.SECKILL.value.equals(productInfo.getType())) {
            if (Objects.isNull(productInfo.getBeginTime()) || Objects.isNull(productInfo.getKillPrice())) {
                throw new ServiceException(ResultCode.CODE_400, "秒杀价和秒杀开始时间不能为空！");
            }
            productInfo.setAgentPrice(productInfo.getKillPrice());
            productInfo.setCompanyPrice(productInfo.getKillPrice());
        } else if (ProductTypeEnum.FREE.value.equals(productInfo.getType())) {
            productInfo.setAgentPrice(productInfo.getPrice());
            productInfo.setCompanyPrice(productInfo.getPrice());
        }
        return ResponseResult.success(productInfoService.updateById(productInfo));
    }
	
	@ApiOperation("删除商品信息表接口")
    @PostMapping("/deleteByIds")
    public ResponseResult<Boolean> deleteByIds(@RequestParam List<Long> ids) {
        return ResponseResult.success(productInfoService.removeByIds(ids));
    }

    @ApiOperation("上架/下架")
    @PostMapping("/changeStatus")
    public ResponseResult<Boolean> changeStatus(@RequestParam Long id) {
        return ResponseResult.success(productInfoService.changeStatus(id));
    }

    @ApiOperation("商品信息表列表接口")
    @PostMapping("/findProductList")
    @NoCheckToken
    public ResponseResult<List<ProductInfo>> findProductList(@RequestParam(required = false) String searchKey,
                                                             @RequestParam(required = false) String orderBy,
                                                             @RequestParam(required = false) Integer level) {
        if (StrUtil.isBlank(orderBy)) {
            orderBy = "a.sort asc";
        }
        return ResponseResult.success(productInfoService.findProductList(searchKey, orderBy, level));
    }
}
